// 引入Mock
// const Mock = require("mockjs");
import Mock, { Random as MockRandom } from "mockjs";
//获取mock.Random对象
// const MockRandom = Mock.Random;
// 设置拦截ajax请求的响应时间
Mock.setup({
  timeout: "200-400"
});
const vCode = "123456";

// 验证码模拟
// eslint-disable-next-line no-unused-vars
function getVerificatCode(prarms) {
  const prarmsObj = JSON.parse(prarms.body);
  return Object.assign(prarmsObj, { vCode: vCode });
}

// 登录模拟
// eslint-disable-next-line no-unused-vars
function loginFun(prarms) {
  const prarmsObj = JSON.parse(prarms.body);
  if (prarmsObj.code === vCode) {
    return { code: 1, text: "登陆成功" };
  } else {
    return { code: 2, text: "验证码出错，登录失败" };
  }
}

// 生成随机表单
const getMockData = function() {
  let list = [];
  for (let i = 0; i < 10; i++) {
    let newObj = {
      name: MockRandom.cname(), // 随机生成一个常见的中文名字
      cfirst: MockRandom.cfirst(), // 模拟姓氏
      clast: MockRandom.clast(), // 模拟名字
      natural: MockRandom.natural(10, 30), //随机自然数10~30
      age: MockRandom.csentence(5, 10), //随机生成一条中文句子（min,max）
      data: MockRandom.date("yyyy-MM-dd"), //随机生成日期
      time: MockRandom.time(), //时间
      image: MockRandom.image("200x100", "#4A7BF7", "Hello"), // 模拟图片
      city: MockRandom.city(true), //生成随机城市
      province: MockRandom.province(), //生成随机省份
      url: MockRandom.url(), // 生成随机url
      ip: MockRandom.ip() // 生成随机ip地址
    };
    list.push(newObj);
  }
  return list;
};

// Mock.mock(url,post/get,返回的数据);
Mock.mock("/getVerificatCode", "post", getVerificatCode); //获取验证码
Mock.mock("/login", "post", loginFun); // 登录
Mock.mock("/api/data", "get", getMockData); //随机表单
